<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            height: 5000px;
        }
        div{
            height: 100px;
            background-color: red;
        }
        button{
            width: 50px
            ;
            position: fixed;
            right: 0;
            bottom:20px;
          
            display: none;
        }
    </style>
</head>
<body>
    <div>顶部</div>
    <button id="btn">回到顶部</button>
    <script>
        onscroll=function(){
            if(scrollY>=1000){
                btn.style.display="block"
            }else{
                btn.style.display="none"
            }
        }
        var mytimer=null
        btn.onclick=function(){
        
        if(mytimer==null){
            mytimer=setInterval(function(){
                
                if(scrollY<=0){
                    clearInterval(mytimer)
                    mytimer=null
                }
                scrollTo({top:scrollY-100})
            },50)
            
        } 
                }
          
    </script>
</body>
</html>